import ElementUI from 'element-ui'
import axios from 'axios';

var ajax = axios.create({
    baseURL: process.env.BASE_URL,
    headers: {
        APPID: 1,
    },
    withCredentials: true, //cookie
    crossDomain: true //跨域
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config) {
    //在请求发出之前进行一些操作
    if (sessionStorage.getItem('jwt')) {
        config.headers['TOKEN'] = sessionStorage.getItem('jwt');
    }
    return config;
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '请求发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with request error
    return Promise.reject(err);
});
//添加一个响应拦截器
ajax.interceptors.response.use(function(res) {
    //在这里对返回的数据进行处理
    if (!res.data.status) {
        if (res.data.errorCode == 401) {
            location.href = '/login'
        }
        ElementUI.Message.error({
            message: res.data.message,
            type: 'error'
        });
        throw new Error(res.data.message);
    }

    return (res.data.data ? res.data.data : res.data.status);
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '响应发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with response error
    return Promise.reject(err);
})


//管理员自身
export const adminLogin = params => {
    return ajax.post('adminControlPanel', params);
};
export const updatePwd = params => {
    return ajax.post('adminControlPanel/updatePassword', params);
};

//管理员操作
export const getAdminLists = params => {
    return ajax.get('admin', params);
}
export const addAdmin = params => {
    return ajax.post('admin', params);
}
export const deleteAdminById = adminId => {
    return ajax.delete('admin/' + adminId);
}
export const getAdminInfoById = adminId => {
    return ajax.get('admin/' + adminId);
}
export const updateAdminById = (adminId, params) => {
    return ajax.put('admin/' + adminId, params);
}


//文件上传 前台文件需要设置一个path属性
export const uploadFiles = params => {
    return ajax.post('upload' + params.path, params);
};

//自定义数据
export const getConfig = keys => {
    return ajax.get('config?keys=' + keys);
}
export const setConfig = params => {
    return ajax.post('config', params);
}

//聊天消息
export const getMessagesList = params => {
    return ajax.get('message', params);
}
export const postMessage = params  => {
    return ajax.post('message', params);
}
export const messagePass = messageId  => {
    return ajax.put('message/' + messageId);
}
export const deleteMessageById = messageId  => {
    return ajax.delete('message/' + messageId);
}

//直播banner
export const getLiveBannerList = parms => {
    return ajax.get('liveBanner', parms);
};
export const addLiveBanner = params => {
    return ajax.post('liveBanner', params);
};
export const updateLiveBanner = params => {
    return ajax.put('liveBanner/' + params.bannerId, params);
};
export const deleteLiveBanner = bannerId => {
    return ajax.delete('liveBanner/' + bannerId);
};

//在线用户
export const getOnlineRobotList = () => {
    return ajax.get('onlineMember/robot');
};
export const getOnlineRealList = () => {
    return ajax.get('onlineMember/real');
};

export const setOnlineUser = params => {
    return ajax.put('onlineMember/robot', params);
};

//客户管理
export const getUserList = params => {
    return ajax.get('user', params);
};
export const addUser = params => {
    return ajax.post('user', params);
};
export const updateUser = params => {
    return ajax.put('user/' + params.userId, params);
};
export const deleteUser = userId => {
    return ajax.delete('user/' + userId);
};


//直播时段
export const getLiveTime = () => {
    return ajax.get('liveTime');
};
export const addLiveTime = params => {
    return ajax.post('liveTime', params);
};
export const updateLiveTime = params => {
    return ajax.put('liveTime/' + params.liveTimeId , params);
};
export const deleteLiveTime = liveTimeId  => {
    return ajax.delete('liveTime/' + liveTimeId );
};

//用户观看时段
export const getUserTime = params => {
    return ajax.get('userTime', params);
};
